package com.leetcode.demo13;

public class Solution {
    private int getVal(char c){
        switch (c){
            case 'M':
                return 1000;
            case 'D':
                return 500;
            case 'C':
                return 100;
            case 'L':
                return 50;
            case 'X' :
                return 10;
            case 'V':
                return 5;
            case 'I':
                return 1;
        }
        throw new IllegalArgumentException("unsupported character");
    }

    public int romanToInt(String s) {
        int res = 0;
        if(s.length() == 0) return res;
        for (int i = 0; i < s.length() - 1; i++) {   //边界条件没有必要判断最后一位
            int cur = getVal(s.charAt(i));  //得到较靠左的值
            int nex = getVal(s.charAt(i+1)); //得到靠右的值
            if(cur < nex){   //靠左的值 < 靠右的值
                res -= cur;  //需要减掉
            }else{
                res += cur;  //左值 > 右值 正常情况，加上就好
            }
        }
        return res + getVal(s.charAt(s.length()-1));  //加上最后一位的值
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int res = solution.romanToInt("IV");
        System.out.println(res);

    }


}
